package com.sd.repay.task;

import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.pojo.PayOrder;
import com.sd.repay.service.PayOrderService;
import com.sd.repay.service.QuickPayService;
import com.sd.repay.utils.DateUtil;
import com.sd.repay.utils.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 同步交易状态任务
 * Created by sd on 2018/08/28.
 */
@Component
public class SyncPayOrderTask {
    private static Logger log = LoggerFactory.getLogger(SyncPayOrderTask.class);

    @Autowired
    private PayOrderService payOrderService;

    /**
     * 根据订单表未知或交易中的订单去对应通道同步状态
     */
    public void executeSyncPayOrder() {
        log.info("--------上游同步订单状态定时任务开始执行---------");
        try {
            List<PayOrder> unEndStatusOrders = payOrderService.getUnEndStatusPayOrder();
            if (CollectionUtils.isEmpty(unEndStatusOrders)) {
                log.info("没有需要同步状态的订单");
                return;
            }
            log.info("--------需要同步状态的订单记录数-------------" + unEndStatusOrders.size());

            QuickPayService quickPayService = null;
            String orderNo = "", resOrderNo = "", acqCode = "", acqOrderNo = "", transTime = "", transStatus = "", resCode = "", resMsg = "";
            Map<String, String> queryResMap = null;

            for (PayOrder order : unEndStatusOrders) {

                orderNo = order.getOrderNo();
                acqCode = order.getAcqCode();
                log.info("--------需要同步状态的订单号/通道编码-------------" + orderNo + "/" + acqCode);

                quickPayService = WebUtil.getQuickPayServiceByAcqCode(acqCode);
                if (null == quickPayService) {
                    log.info("订单号" + orderNo + "对应通道" + acqCode + "不合法");
                    return;
                }

                CommonResult queryRes = quickPayService.queryOrderInfo(order);
                if (null != queryRes && queryRes.getStatus() == 200 && null != queryRes.getData()) {

                    queryResMap = (Map<String, String>) queryRes.getData();
                    resOrderNo = queryResMap.get("orderNo");
                    acqOrderNo = queryResMap.get("acqOrderNo");
                    transTime = queryResMap.get("transTime");
                    resCode = queryResMap.get("resCode");
                    resMsg = queryResMap.get("resMsg");
                    transStatus = queryResMap.get("transStatus");

                    transTime = StringUtils.isBlank(transTime) ? DateUtil.getDateTime() : transTime;
                    resMsg = "订单异步查询：" + (StringUtils.isBlank(resMsg) ? "" : resMsg);

                    if (orderNo.equals(resOrderNo) && StringUtils.isNotBlank(transStatus)) {
                        int updateRow = payOrderService.writeBackPayOrderInfo(orderNo, acqOrderNo, transTime, transStatus, null, resCode, resMsg);
                        log.info("订单号" + orderNo + "同步结果修改订单状态影响的行数updateRow=" + updateRow);
                        if ("2".equals(transStatus) || "3".equals(transStatus)) {
                            updateRow = payOrderService.updatePayOrderNotifyStatus(orderNo, Constants.IS_NEED_NOTIFY);
                            log.info("订单号" + orderNo + "同步结果状态为" + transStatus + "修改需要通知状态影响的行数updateRow=" + updateRow);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("-----------同步订单状态定时任务完成--------------");
        WebUtil.removeBlockTaskBySpringId(WebUtil.getClassSimpleName(this.getClass()));

    }

}
